/*
@author: lyf
@file: s.go
@time: 2022/10/14 16:23
*/
package main

import (
	"fmt"
	"strconv"
)
var username = ""
var userpasswd = ""
var usermoney = ""
var u = &User{name: username,
	passwd: userpasswd,
	balance: usermoney,
}

var atm = &ATM{bank: "银行",
	balance: "8000",
	user: map[string]map[string]string{username:{userpasswd:usermoney}},
}

var money string

type User struct {
	name string
	passwd string
	balance string
}

type ATM struct {
	bank string
	balance string
	user map[string]map[string]string
}

func (atm *ATM) moveMoney()int{
	atmmoney, _ := strconv.Atoi(money)
	ubalance,_ := strconv.Atoi(u.balance)
	atmbalance,_ := strconv.Atoi(atm.balance)
	if atmmoney > ubalance{
		fmt.Println("您的余额不足")
	}
	if atmmoney > atmbalance{
		fmt.Println("atm余额不足")
	} else {
		ubalance -= atmmoney
		atmbalance -= atmmoney
		u.balance = strconv.Itoa(ubalance)
		atm.balance = strconv.Itoa(atmbalance)
		fmt.Println("您的余额为",u.balance)
	}
	return ubalance
}

func (atm *ATM) saveMoney()int {
	atmmoney, _ := strconv.Atoi(money)
	ubalance,_ := strconv.Atoi(u.balance)
	atmbalance,_ := strconv.Atoi(atm.balance)
	ubalance += atmmoney
	atmbalance += atmmoney
	u.balance = strconv.Itoa(ubalance)
	atm.balance = strconv.Itoa(atmbalance)
	fmt.Println("您的余额为",u.balance)
	return ubalance
}

func main()  {
	choice := ""
	loginname := ""
	loginpasswd := ""
	ALoop:
	for {
		fmt.Println("请输入您的选择:")
		fmt.Println("1.登录")
		fmt.Println("2.注册")
		fmt.Println("q.退出")
		_,_ = fmt.Scanln(&choice)
		if choice == "1"{
			println("请输入用户名")
			_,_ = fmt.Scanln(&loginname)
			if loginname == u.name{
				println("请输入密码")
				_,_= fmt.Scanln(&loginpasswd)
				if loginpasswd == u.passwd {
					fmt.Println("欢迎进入本银行")
					op := ""
				Loop:
					for {
						fmt.Println("请输入您的选择:")
						fmt.Println("1.存钱")
						fmt.Println("2.取钱")
						fmt.Println("3.查看余额")
						fmt.Println("4.退出")
						_, _ = fmt.Scanln(&op)
						if op == "1" {
							fmt.Println("请输入您需要存入的金额")
							_,_ = fmt.Scanln(&money)
							atm.saveMoney()
							goto Loop
						}
						if op == "2" {
							fmt.Println("请输入您需要取出的金额")
							_,_ = fmt.Scanln(&money)
							atm.moveMoney()
							goto Loop
						}
						if op == "3" {
							fmt.Println(u.balance)
							goto Loop
						}
						if op == "4" {
							goto ALoop
						}
					}
				}
			}
		}
		if choice == "2"{
			println("请输入用户名")
			_,_ = fmt.Scanln(&username)
			println("请输入密码")
			_,_ = fmt.Scanln(&userpasswd)
			println("请输入初始金额")
			_,_ = fmt.Scanln(&usermoney)
			u =&User{name: username,
			passwd: userpasswd,
			balance: usermoney,
			}
			goto ALoop
		}
		if choice == "q"{
			break
		}
	}
}




